package secondriver.heathfood.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.*;
import butterknife.Bind;
import butterknife.ButterKnife;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import secondriver.heathfood.HeathFoodApplication;
import secondriver.heathfood.R;
import secondriver.heathfood.adapter.FoodClassListAdapter;
import secondriver.heathfood.adapter.FoodClassSpinnerListAdapter;
import secondriver.heathfood.client.ApiClient;
import secondriver.heathfood.client.Constants;
import secondriver.heathfood.bean.FoodClass;
import secondriver.heathfood.common.*;
import secondriver.heathfood.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : secondriver
 */
public class FoodClassActivity extends AbstractBaseActivity {

    private static final LogUtil logUtil = LogUtil.getLogger(FoodClassActivity.class.getName());

    static final int LoadingWhat = 0x01;
    static final int StartBrief = 0x10;

    @Bind(R.id.food_class_ListView)
    SwipeMenuListView mListView;

    @Bind(R.id.food_class_top_level_spinner)
    Spinner mFoodClassSpinner;

    private FoodClassListAdapter mSpinnerAdapter;
    private List<FoodClass> mParentDataList;

    private FoodClassListAdapter mFoodClassAdapter;
    private List<FoodClass> mSubDataList = new ArrayList<FoodClass>();

    private Handler mHandler;
    private ApiClient mApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_class);
        ButterKnife.bind(this);
        setActionBarTitle(R.string.heath_food_class);

        initVariable();
        initListener();
        initHandler();
    }


    private void initVariable() {
        Intent intent = getIntent();
        String foodClassJson = intent.getStringExtra(Constants.FOOD_CLASS_CONTENT_KEY);

        mParentDataList = ApiClient.jsonConvertFoodClassCollection(foodClassJson);
        mSpinnerAdapter = new FoodClassSpinnerListAdapter(this, mParentDataList);

        mFoodClassSpinner.setAdapter(mSpinnerAdapter);
        mFoodClassAdapter = new FoodClassListAdapter(FoodClassActivity.this, mSubDataList);
        mApiClient = ApiClient.build(this, Constants.FOOD_CLASS_CACHE_NAME);
    }


    private void initConentListView() {
        // step 1. create a MenuCreator
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(dp2px(90));
                // set item title
                openItem.setTitle("Open");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getApplicationContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(dp2px(90));
                // set a icon
                deleteItem.setIcon(android.R.drawable.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        mListView.setMenuCreator(creator);


        // step 2. listener item click event
        mListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public void onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        break;
                    case 1:
                        break;
                    default:
                }
            }
        });

        // set SwipeListener
        mListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {

            @Override
            public void onSwipeStart(int position) {
                // swipe start
            }

            @Override
            public void onSwipeEnd(int position) {
                // swipe end
            }
        });

        // other setting
        mListView.setCloseInterpolator(new BounceInterpolator());

        mListView.setAdapter(mFoodClassAdapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FoodClass foodClass = (FoodClass) parent.getItemAtPosition(position);
                if (!foodClass.getCookclass().equals(Constants.CLASSIFY_ROOT + "")) {
                    Message message = Message.obtain();
                    message.what = StartBrief;
                    message.arg1 = foodClass.getId();
                    mHandler.sendMessage(message);
                } else {
                    backstageLoadData(foodClass.getId());
                }
            }
        });
    }

    private void initListener() {
        initConentListView();

        mFoodClassSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                backstageLoadData(id);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }

    private void backstageLoadData(long classifyId) {
        final Message message = Message.obtain(mHandler);
        message.what = LoadingWhat;
        final Bundle bundle = new Bundle();
        bundle.putLong(Constants.CLASSIFY_ID, classifyId);
        BackgroundTaskExecutor.doTask(bundle, message, new BackgroundTasks.LoadFoodClass(mApiClient));
    }

    private void initHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case LoadingWhat:
                        mSubDataList.clear();
                        mSubDataList.addAll(ApiClient.jsonConvertFoodClassCollection((String) msg.obj));
                        mFoodClassAdapter.notifyDataSetChanged();
                        break;
                    case StartBrief:
                        Intent intent = new Intent();
                        intent.setClass(FoodClassActivity.this, FoodBriefActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt(Constants.CLASSIFY_ID, msg.arg1);
                        startActivity(intent, bundle);
                        break;
                    default:
                        break;
                }
            }
        };
    }


    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }
}